package com.controller;

import com.beans.ErrorStatusEnum;
import com.common.entity.R;
import com.common.utils.UploadFileUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 富文本编辑器
 *
 * @author gm
 * @date 2020/6/4
 */
@RestController
@RequestMapping("/ckUpload")
public class CkEditorController {

    @Resource
    private UploadFileUtil uploadFileUtil;

    /**
     * 富文本的图片上传
     *
     * @param file 上传的文件
     * @return java.util.Map
     * @author gm
     * @date 2020/6/4
     */
    @PostMapping("uploadPicture")
    public Map uploadPicture(@RequestParam("upload") MultipartFile file) {
        Map resultMap = new HashMap(4);
        R r = uploadFileUtil.uploadPicture(file, "4");
        if (ErrorStatusEnum.OK.code().equals(r.get(R.CODE))) {
            resultMap.put("uploaded", "1");
            //文件存放的相对路径
            String filePath = (String) r.get(R.DATA);
            String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
            resultMap.put("url", filePath);
            resultMap.put("fileName", fileName);
        } else {
            resultMap.put("uploaded", "0");
            Map object = new HashMap(2);
            object.put("message", r.get(R.MESSAGE));
            resultMap.put("error", object);
        }
        return resultMap;
    }
}
